﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Small.PinBan.PB.View;
using SmallERP.MODEL.PBModel;
using SmallERP.PinBan.PB.Presenter;
using SmallERP.Bll.PinBan;
using SmallERP.MODEL.FoundationModel;
using Smallerp.Contracts;
using Smallerp.Contracts.DataContracts;


namespace Small.PinBan.PB.Froms
{
    public partial class FrmTemplateManager : Form
    {
        public FrmTemplateManager(IOperateDB inOPer)
        {
            InitializeComponent();
            _OperateDB = inOPer;
            _bll = new BoardTemplateBLL(_OperateDB);
        }
        IOperateDB _OperateDB;
        BoardTemplateBLL _bll ;
        private void button1_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text == "添加")
            {
                btnAdd.Text = "保存";
                btnCancel.Enabled = true;
                btnDel.Enabled = false;
                btnEdit.Enabled = false;
                groupBox1.Enabled = true;

                txtTemplateName.Text = string.Empty;

                cmbPaperFace.DataSource = _OperateDB.GetAllPaperFace();
                cmbPaperFace.DisplayMember = "PaperFaceName";
                cmbPaperFace.ValueMember = "PaperFaceId";

                cmbPaperType.DataSource = _OperateDB.GetAllPaperType();
                cmbPaperType.DisplayMember = "PaperTypeName";
                cmbPaperType.ValueMember = "PaperTypeId";

                txtHeight.Value = 1;
                txtLength.Value = 1;

            }
            else
            {
                dxErrorProvider1.SetError(null, "");

                if (string.IsNullOrEmpty(txtTemplateName.Text))
                {
                    dxErrorProvider1.SetError(txtTemplateName, "不能为空");
                    return;
                }
                if ((int)cmbPaperFace.SelectedValue < 1)
                {
                    dxErrorProvider1.SetError(cmbPaperFace, "不能为空");
                    return;
                }
                if ((int)cmbPaperType.SelectedValue < 1)
                {
                    dxErrorProvider1.SetError(cmbPaperType, "不能为空");
                    return;
                }
                if (txtHeight.Value < 0)
                {
                    dxErrorProvider1.SetError(txtHeight, "不能小于0");
                    return;
                }
                if (txtLength.Value < 0)
                {
                    dxErrorProvider1.SetError(txtLength, "不能小于0");
                    return;
                }
                if (DataTransfer.BoardTempalteList.FirstOrDefault(t => t.TemplateName == txtTemplateName.Text.Trim()) != null)
                {
                    dxErrorProvider1.SetError(txtTemplateName, "模板名称已经存在，请重新输入名称");
                }

                BoardTemplate_DCT newT = new BoardTemplate_DCT()
                {
                    BarCodeStatus = (SmallERP.COM.Status.PhotoBarCodeStatus)radioGroup1.EditValue,
                    Height = (float)txtHeight.Value,
                    PaperFaceId = (int)cmbPaperFace.SelectedValue,
                    PaperTypeId = (int)cmbPaperType.SelectedValue,
                    PrinterId = DataTransfer.CurPrinerInfo.PrinterID,
                    TemplateName = txtTemplateName.Text.Trim(),
                    MinPhotoSize = spinEditMinSize.Value,
                    Length = (float)txtLength.Value

                };

                int result = _bll.AddNewTemplate(newT);
                if (result == 1)
                {
                    DataTransfer.BoardTempalteList = _bll.GetAllTemplate(DataTransfer.CurPrinerInfo);
                    BindCombox1();
                    MessageBox.Show("添加成功！");               
                }
                else
                {
                    MessageBox.Show("添加失败");
                }
                btnAdd.Text = "添加";
                btnCancel.Enabled = false;
                btnDel.Enabled = true;
                btnEdit.Enabled = true;
                groupBox1.Enabled = false;

                //更新cmbTemplate列表
            }
        }

        private void FrmTemplateManager_Load(object sender, EventArgs e)
        {
            BindCombox1();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnAdd.Text = "添加";
            btnEdit.Text = "编辑";
            btnCancel.Text = "取消";
            btnAdd.Enabled = true;
            btnCancel.Enabled = false;
            btnDel.Enabled = true;
            btnEdit.Enabled = true;
            groupBox1.Enabled = false;
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if ((int)comboBox1.SelectedValue > 0)
            {
                if (MessageBox.Show("确定要删除" + comboBox1.Text, "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    int result = _bll.DelTempalte((int)comboBox1.SelectedValue);
                    if (result == 1)
                    {
                        DataTransfer.BoardTempalteList.Remove(DataTransfer.BoardTempalteList.FirstOrDefault(t => t.TemplateId == (int)comboBox1.SelectedValue));
                        BindCombox1();
                        MessageBox.Show("删除成功");
                    }
                    else
                    {
                        MessageBox.Show("删除失败");
                    }
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if ((int)comboBox1.SelectedValue < 1)
            {
                return;
            }

            BoardTemplate_DCT curT = DataTransfer.BoardTempalteList.FirstOrDefault(t => t.TemplateId == (int)comboBox1.SelectedValue);
            if (btnEdit.Text == "编辑")
            {
                btnEdit.Text = "保存";
                btnCancel.Enabled = true;
                btnDel.Enabled = false;
                btnAdd.Enabled = false;
                groupBox1.Enabled = true;

                cmbPaperFace.DataSource = _OperateDB.GetAllPaperFace();
                cmbPaperFace.DisplayMember = "PaperFaceName";
                cmbPaperFace.ValueMember = "PaperFaceId";
                cmbPaperFace.SelectedValue = curT.PaperFaceId;

                cmbPaperType.DataSource = _OperateDB.GetAllPaperType();
                cmbPaperType.DisplayMember = "PaperTypeName";
                cmbPaperType.ValueMember = "PaperTypeId";
                cmbPaperType.SelectedValue = curT.PaperTypeId;

            }
            else//保存
            {
                dxErrorProvider1.SetError(null, "");

                if (string.IsNullOrEmpty(txtTemplateName.Text))
                {
                    dxErrorProvider1.SetError(txtTemplateName, "不能为空");
                    return;
                }
                if ((int)cmbPaperFace.SelectedValue < 1)
                {
                    dxErrorProvider1.SetError(cmbPaperFace, "不能为空");
                    return;
                }
                if ((int)cmbPaperType.SelectedValue < 1)
                {
                    dxErrorProvider1.SetError(cmbPaperType, "不能为空");
                    return;
                }
                if (txtHeight.Value < 0)
                {
                    dxErrorProvider1.SetError(txtHeight, "不能小于0");
                    return;
                }
                if (txtLength.Value < 0)
                {
                    dxErrorProvider1.SetError(txtLength, "不能小于0");
                    return;
                }
                if (DataTransfer.BoardTempalteList.FirstOrDefault(t => t.TemplateName == txtTemplateName.Text.Trim()) != null)
                {
                    dxErrorProvider1.SetError(txtTemplateName, "模板名称已经存在，请重新输入名称");
                }

                curT.BarCodeStatus = (SmallERP.COM.Status.PhotoBarCodeStatus)radioGroup1.EditValue;
                curT.Height = (float)txtHeight.Value;
                curT.Length = (float)txtLength.Value;
                curT.PaperFaceId = (int)cmbPaperFace.SelectedValue;
                curT.PaperTypeId = (int)cmbPaperType.SelectedValue;
                curT.PrinterId = DataTransfer.CurPrinerInfo.PrinterID;
                curT.TemplateName = txtTemplateName.Text.Trim();
                curT.MinPhotoSize = spinEditMinSize.Value;
                int result = _bll.ModifyTemplate(curT);
                if (result == 1)
                {
                    BindCombox1();
                    MessageBox.Show("更新成功！");
                }
                else
                {                  
                    //重新加载
                    DataTransfer.BoardTempalteList = _bll.GetAllTemplate(DataTransfer.CurPrinerInfo);
                    BindCombox1();
                    MessageBox.Show("更新失败");
                }
                btnAdd.Text = "添加";
                btnEdit.Text = "编辑";
                btnCancel.Text = "取消";
                btnAdd.Enabled = true;
                btnCancel.Enabled = false;
                btnDel.Enabled = true;
                btnEdit.Enabled = true;
                groupBox1.Enabled = false;

                //更新cmbTemplate列表
            }
        }

        /// <summary>
        /// 绑定主模板列表
        /// </summary>
        private void BindCombox1()
        {
            comboBox1.DataSource=null;
            comboBox1.DataSource = DataTransfer.BoardTempalteList;
            comboBox1.DisplayMember = "TemplateName";
            comboBox1.ValueMember = "TemplateId";
            if (DataTransfer.BoardTempalteList.Count>0)
            {
                comboBox1.SelectedIndex = 0;
                BoardTemplate_DCT curT = DataTransfer.BoardTempalteList.FirstOrDefault(t => t.TemplateId == (int)comboBox1.SelectedValue);

                txtTemplateName.Text = curT.TemplateName;
                cmbPaperFace.Text = curT.PaperFaceName;
                cmbPaperType.Text = curT.PaperTypeName;
                txtHeight.Value = (decimal)curT.Height;
                txtLength.Value = (decimal)curT.Length;
                radioGroup1.EditValue = (int)curT.BarCodeStatus;
                spinEditMinSize.Value = (decimal)curT.MinPhotoSize;
            }                
        }

        private void comboBox1_DropDownClosed(object sender, EventArgs e)
        {
            if (comboBox1.SelectedValue == null || (int)comboBox1.SelectedValue < 1)
            {
                return;
            }
            BoardTemplate_DCT curT = DataTransfer.BoardTempalteList.FirstOrDefault(t => t.TemplateId == (int)comboBox1.SelectedValue);

            txtTemplateName.Text = curT.TemplateName;
            cmbPaperFace.Text = curT.PaperFaceName;
            cmbPaperType.Text = curT.PaperTypeName;
            txtHeight.Value = (decimal)curT.Height;
            txtLength.Value = (decimal)curT.Length;
            radioGroup1.EditValue = (int)curT.BarCodeStatus;
            spinEditMinSize.Value = (decimal)curT.MinPhotoSize;

        }

        private void label11_Click(object sender, EventArgs e)
        {

        }
    }
}
